package com.mjk.rest.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mjk.common.rest.BaseController;
import com.mjk.common.util.TreeUtil;
import com.mjk.constant.AdminCommonConstant;
import com.mjk.model.system.Menu;
import com.mjk.service.system.MenuService;
import com.mjk.vo.MenuTree;

import tk.mybatis.mapper.entity.Example;

@RestController
@RequestMapping("api/menu")
public class MenuController extends BaseController<MenuService, Menu>{

	@RequestMapping(value="tree", method = RequestMethod.GET)
	public List<MenuTree> getTree(String name){
		Example example = new Example(Menu.class);
		if(StringUtils.isNotBlank(name)){
			example.createCriteria().andLike("title", "%" + name + "%");
		}
		return getMenuTree(baseBiz.selectByExample(example), AdminCommonConstant.ROOT);
	}
	
	private List<MenuTree> getMenuTree(List<Menu> list, int root){
		List<MenuTree> trees = new ArrayList<MenuTree>();
		MenuTree node = null;
		for (Menu menu : list) {
			node = new MenuTree();
			BeanUtils.copyProperties(menu, node);
			node.setLabel(menu.getTitle());
			trees.add(node);
		}
		return TreeUtil.bulid(trees, root);
	}
}
